package constellation.Activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.google.gson.Gson;

import java.util.Map;

import constellation.custom.CircleImageView;
import constellation.util.AssetsUtil;
import constellation.util.HttpUtils;
import constellation.util.ParnterBean;
import constellation.util.URLutils;

public class ParntnerInfoActivity extends AppCompatActivity {


    private ImageView imgBack;
    private TextView tvTitle;
    private CircleImageView imgBoy;
    private TextView tvStarBoyName;
    private CircleImageView imgGirl;
    private TextView tvStarGirlName;
    private TextView tvStar;
    private TextView tvStar2;
    private TextView tvZhishu;
    private TextView tvJieguo;
    private TextView tvBizhong;
    private TextView tvAdvise;
    private TextView tvZhuyi;
    String men,mens,women,womens,parnterURL;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==1) {
                String json=(String)msg.obj;
                Gson gson=new Gson();
                ParnterBean parnterBean=gson.fromJson(json,ParnterBean.class);
                ParnterBean.ResultBean resultBean=parnterBean.getResult();

                tvZhishu.setText("配对评分:"+resultBean.getZhishu());
                tvJieguo.setText(resultBean.getJieguo());
                tvBizhong.setText("星座比重"+resultBean.getBizhong());
                tvAdvise.setText(resultBean.getLianai());
                tvZhuyi.setText(resultBean.getZhuyi());
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parntner_info);
        initView();
        initData();
        //获取接口数据
        parnterURL =  URLutils.getParenterUrl(mens,womens,"70c808f67e31c5ec4c7700bec866de7b","http://apis.juhe.cn/xzpd/query");

        loadData();
    }
    /*
    开启新线程
     */
    public void  loadData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //执行网络请求
                String json= HttpUtils.getJsonByNetwork(parnterURL);
                //将子线程获取到的数据，通过handle对象传递到主线程
                Message msg=handler.obtainMessage();
                msg.what=1;
                msg.obj=json;
                handler.sendMessage(msg);
            }
        }).start();
    }
public  void initData(){
     men = getIntent().getStringExtra("man");
     women = getIntent().getStringExtra("woman");
    String men_logo=getIntent().getStringExtra("men_logo");
    String women_logo=getIntent().getStringExtra("women_logo");
    tvStarBoyName.setText(men);
    tvStarGirlName.setText(women);
    tvStar.setText("星座配对："+men+women+"配对");
    tvStar2.setText(men+"VS"+women);
      mens=men.replace("座","");
     womens=women.replace("座","");

    Map<String, Bitmap> contentlogoImgMap=AssetsUtil.getContentlogoImgMap();
    Bitmap menbm=contentlogoImgMap.get(men_logo);
    Bitmap womenbm=contentlogoImgMap.get(women_logo);
    imgBoy.setImageBitmap(menbm);
    imgGirl.setImageBitmap(womenbm);
}
    private void initView() {

        imgBack = (ImageView) findViewById(R.id.img_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        imgBoy = (CircleImageView) findViewById(R.id.img_boy);
        tvStarBoyName = (TextView) findViewById(R.id.tv_starBoyName);
        imgGirl = (CircleImageView) findViewById(R.id.img_girl);
        tvStarGirlName = (TextView) findViewById(R.id.tv_starGirlName);
        tvStar = (TextView) findViewById(R.id.tv_star);
        tvStar2 = (TextView) findViewById(R.id.tv_star2);
        tvZhishu = (TextView) findViewById(R.id.tv_zhishu);
        tvJieguo = (TextView) findViewById(R.id.tv_jieguo);
        tvBizhong = (TextView) findViewById(R.id.tv_bizhong);
        tvAdvise = (TextView) findViewById(R.id.tv_advise);
        tvZhuyi = (TextView) findViewById(R.id.tv_zhuyi);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}